package io.renren.service;

import io.renren.dao.SysGeneratorDao;
import io.renren.entity.ColumnEntity;
import io.renren.entity.TableEntity;
import io.renren.utils.GenUtils;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午3:33:38
 */
@Service
public class SysGeneratorService {
	@Autowired
	private SysGeneratorDao sysGeneratorDao;

	public List<Map<String, Object>> queryList(Map<String, Object> map) {
		return sysGeneratorDao.queryList(map);
	}

	public int queryTotal(Map<String, Object> map) {
		return sysGeneratorDao.queryTotal(map);
	}

	public Map<String, String> queryTable(String tableName) {
		return sysGeneratorDao.queryTable(tableName);
	}

	public List<Map<String, String>> queryColumns(String tableName) {
		return sysGeneratorDao.queryColumns(tableName);
	}

	public byte[] generatorCode(String s) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		List<String> tableNames = new ArrayList<String>();
		tableNames.add(s);
				
		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			//生成代码
			GenUtils.generatorCode(table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}
	public byte[] generatorCode2(String s) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		List<String> tableNames = new ArrayList<String>();
		tableNames.add(s);
		
		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			//生成代码
			GenUtils.generatorCode2(table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public byte[] generatorCode3(String s) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		List<String> tableNames = sysGeneratorDao.queryDatabase(s);

		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			//生成代码
			GenUtils.generatorCode2(table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public byte[] generatorCode4(String s,String template) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		List<String> tableNames = sysGeneratorDao.queryDatabase(s);

		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			//生成代码
			GenUtils.generatorCodeView(table, columns, zip,template);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public byte[] generatorView(String s,String template) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		List<String> tableNames = sysGeneratorDao.queryDatabase(s);

		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			//生成代码
			GenUtils.generatorView(table, columns, zip,template);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public byte[] generatorMenu(String s,String template) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		List<String> tableNames = sysGeneratorDao.queryDatabase(s);
		//生成代码
		GenUtils.generatorMenu(tableNames, zip, template);
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public byte[] generatorMessages(String s,String template) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		List<String> tableNames = sysGeneratorDao.queryDatabase(s);
		Configuration config =GenUtils.getConfig();
		List<TableEntity> listTableEntity=new ArrayList<TableEntity>();
		Map<String, Object> map = new HashMap<String, Object>();
		for(String tableName : tableNames){
			TableEntity tableEntity=new TableEntity();
			//查询表信息
			Map<String, String> table = queryTable(tableName);

			tableEntity.setTableName(table.get("tableName" ));
			tableEntity.setComments(table.get("tableComment" ));
			//表名转换成Java类名
			String className = GenUtils.tableToJava(tableEntity.getTableName(), config.getString("tablePrefix" ));
			tableEntity.setClassName(className);
			tableEntity.setClassname(StringUtils.uncapitalize(className));

			String [] col={"id","attrs","version","archived","created_on","created_by","updated_on","updated_by"};
			List<String> list= Arrays.asList(col);

			List<ColumnEntity> listColumnEntity=new ArrayList<ColumnEntity>();
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			for(Map<String, String> column : columns){
				if(!list.contains(column.get("columnName" ))){
					ColumnEntity columnEntity = new ColumnEntity();
					columnEntity.setColumnName(column.get("columnName" ));
					columnEntity.setComments(column.get("columnComment" ));
					columnEntity.setMessageColumnName(GenUtils.strOneUpperCase(String.valueOf(column.get("columnName"))).replaceAll("_"," "));
					listColumnEntity.add(columnEntity);
				}
			}
			tableEntity.setColumns(listColumnEntity);
			listTableEntity.add(tableEntity);
		}
		map.put("MESSAGES",listTableEntity);
		//生成代码
		GenUtils.generatorCodeMessages(map, zip,template);
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public void codeOutTest(String s,String template) {
		List<String> tableNames = sysGeneratorDao.queryDatabase(s);
		Configuration config =GenUtils.getConfig();
		List<TableEntity> listTableEntity=new ArrayList<TableEntity>();
		Map<String, Object> map = new HashMap<String, Object>();
		for(String tableName : tableNames){
			TableEntity tableEntity=new TableEntity();
			//查询表信息
			Map<String, String> table = queryTable(tableName);

			tableEntity.setTableName(table.get("tableName" ));
			tableEntity.setComments(table.get("tableComment" ));
			//表名转换成Java类名
			String className = GenUtils.tableToJava(tableEntity.getTableName(), config.getString("tablePrefix" ));
			tableEntity.setClassName(className);
			tableEntity.setClassname(StringUtils.uncapitalize(className));

			String [] col={"id","attrs","version","archived","created_on","created_by","updated_on","updated_by"};
			List<String> list= Arrays.asList(col);

			List<ColumnEntity> listColumnEntity=new ArrayList<ColumnEntity>();
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			for(Map<String, String> column : columns){
				if(!list.contains(column.get("columnName" ))){
					ColumnEntity columnEntity = new ColumnEntity();
					columnEntity.setColumnName(column.get("columnName" ));
					columnEntity.setComments(column.get("columnComment" ));
					columnEntity.setMessageColumnName(GenUtils.strOneUpperCase(String.valueOf(column.get("columnName"))).replaceAll("_"," "));
					listColumnEntity.add(columnEntity);
				}
			}
			tableEntity.setColumns(listColumnEntity);
			listTableEntity.add(tableEntity);
		}
		map.put("MESSAGES",listTableEntity);
		//生成代码
		String outFile=GenUtils.generatorCodeMessages111(map, template);
		String file="C:\\Users\\it\\Desktop\\test\\"+"message"+".java";
		outputStreamWriter(file,outFile);
	}

	public void outputStreamWriter (String file,String outFile){
		try{
			File f = new File(file);
			FileOutputStream fop = new FileOutputStream(f);
			// 构建FileOutputStream对象,文件不存在会自动新建

			OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
			// 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk

			writer.append(outFile);
			// 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入

			writer.close();
			// 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉

			fop.close();
			// 关闭输出流,释放系统资源

		}catch (IOException e){
			e.printStackTrace();
		}
	}
}
